package com.its.oa.system.Role.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.its.oa.dao.base.ItsHibernateDaoSupport;
import com.its.oa.model.Role;
import com.its.oa.system.Role.dao.RoleDao;
import com.its.oa.util.EntityValidata;

public class RoleDaoImpl extends ItsHibernateDaoSupport implements RoleDao {

	public Role findById(int id ){
		Role role = (Role) getHibernateTemplate().get(Role.class, id);
		return role;
	}
	@SuppressWarnings("unchecked")
	public List<Role> findRolesByUserName(String username) {
		List<Role> list = getHibernateTemplate().find("select ur.role from UserRole ur " +
				"where ur.user.username like '"+username+"' ");
		return list;
	}

	public void addRole(Role role) {
		getHibernateTemplate().save(role);
	}

	public void delRoleById(Role role) {
		getHibernateTemplate().delete(role);
		
	}

	@SuppressWarnings("unchecked")
	public List<Role> findAllRole() {
		String hql = "from Role";
		List<Role> rolelist = getHibernateTemplate().find(hql);
		return rolelist;
	}

	@SuppressWarnings("unchecked")
	public List<Role> findRoleByPage(int offset, int pagesize) {
		String hql = "from Role";
		List<Role> rolelist = (List<Role>) findByPage(hql, offset, pagesize);
		return rolelist;
	}

	@SuppressWarnings("unchecked")
	public List<Role> queryRoleListByUserid(int userid) {
		String hql = "select ur.role from UserRole ur where ur.user.id="+userid;
		List<Role> roleList = getHibernateTemplate().find(hql);
		return roleList;
	}

	@SuppressWarnings("unchecked")
	public Role findRoleByCode(String rolecode) {
		String hql ="from Role r where r.code like '"+rolecode+"' ";
		List<Role> list = getHibernateTemplate().find(hql);
		if(EntityValidata.isNotEmpty(list)){
			return list.get(0);
		}else
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<Role> queryRoleListNotinRoleids(String roleids) {
		List<Role> list = new ArrayList<Role>();
		if(EntityValidata.isNotEmpty(roleids)){
			list = getHibernateTemplate().find("from Role r where r.id not in ("+roleids+")");
		}else
		{
			list = getHibernateTemplate().find("from Role");
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Role> queryRoleListByIds(String ids) {
		List<Role> list = new ArrayList<Role>();
		if(EntityValidata.isNotEmpty(ids)){
			list = getHibernateTemplate().find("from Role r where r.id in ("+ids+")");
		}else
		{
			list = getHibernateTemplate().find("from Role");
		}
		return list;
	}	
}
